﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNet.Common.Kit;
using System.Windows.Forms;
using Top.Api.Request;
using Top.Api;
using Top.Api.Response;
using Top.Api.Util;
namespace Shop.Image.Kit
{
    public class TaoBaoImage:BaseImage
    {
        string _appKey;
        string _appSecret;
        string _sessionKey;
        string _procUrl;  //回调URL
        ITopClient _client;
        long _dirFileID = 0; //图片空间里文件夹目录ID;可以在上传时用到该变量。
        LogerHelper log = new LogerHelper(DirType.Local, MessageType.Error);

        public TaoBaoImage(string iniSection)
        {
            IniHelper ini = new IniHelper(Application.StartupPath+"\\sys.ini");
            _appKey = ini.GetKeyValue(iniSection, "appKey");
            _appSecret = ini.GetKeyValue(iniSection, "appSecret");
            _sessionKey = ini.GetKeyValue(iniSection, "sessionKey");
            this._client = new DefaultTopClient("http://gw.api.taobao.com/router/rest", _appKey, _appSecret);
        }

        public List<string> GetShopItems()
        {
            ItemsOnsaleGetRequest onsaleReq = new ItemsOnsaleGetRequest();
            onsaleReq.Fields = "num_iid,title,price,picurl,detailurl";
            ItemsOnsaleGetResponse onsaleRes = _client.Execute(onsaleReq, _sessionKey);
            ItemGetRequest spReq = new ItemGetRequest();
            spReq.Fields = "title,price,desc_modules";
            
            foreach (Top.Api.Domain.Item item in onsaleRes.Items)
            {
                long numiid = item.NumIid;
                spReq.NumIid = numiid;
                ItemGetResponse spRes = _client.Execute(spReq, _sessionKey);

            }
            //foreach(onsaleReq
            return null;
        }

        public TaoBaoImage(string iniFilePath, string iniSection, string dirFileName)
        {
            IniHelper ini = new IniHelper(iniFilePath);
            _appKey = ini.GetKeyValue(iniSection, "appKey");
            _appSecret = ini.GetKeyValue(iniSection, "appSecret");
            _sessionKey = ini.GetKeyValue(iniSection, "sessionKey");
            _procUrl = ini.GetKeyValue(iniSection, "procUrl");
            //此处逻辑是为了应对_sessionKey改变登录不上的问题，如果登录不上则用回调重新得到_sessionKey。
            if (_appKey != "" && _appSecret != ""&&_sessionKey!=""&&_procUrl!="")
            {
                this._client = new DefaultTopClient("http://gw.api.taobao.com/router/rest", _appKey, _appSecret);
                PictureCategoryGetRequest imgDirReq = new PictureCategoryGetRequest();
                imgDirReq.PictureCategoryName = dirFileName;
                PictureCategoryGetResponse imgDirRes = _client.Execute(imgDirReq, _sessionKey);
                if (imgDirRes.IsError)   //此时表明没有与服务器联通，可能是sessionKey过期或者其他配置错误。
                {
                    log.LogToFile("TaoBaoImage():"+"sessionKey或者其他appkey参数有错误");
                    SysOperCls.OpenBrowser(_procUrl);
                }
                else    
                {
                    if (imgDirRes.PictureCategories.Count == 0)  //如果没有文件夹。
                    {
                        PictureCategoryAddRequest imgDirAddReq = new PictureCategoryAddRequest();
                        imgDirAddReq.PictureCategoryName = dirFileName;
                        PictureCategoryAddResponse imgDirAddResp = _client.Execute(imgDirAddReq, _sessionKey);
                        if (!imgDirAddResp.IsError)  //没有错误false 
                        {
                            this._dirFileID = imgDirAddResp.PictureCategory.PictureCategoryId;
                            DirFileName = dirFileName;
                        }
                    }
                    else   //有相应文件夹
                    {
                        this._dirFileID = imgDirRes.PictureCategories[0].PictureCategoryId;
                    }
                }
            }
            else
            {
                log.LogToFile("TaoBaoImage():" + "读取ini文件参数错误");
            }
        }

        public override string ImageUpload(string imgFullName, string imgName)
        {
            PictureUploadRequest req = new PictureUploadRequest();
            req.PictureCategoryId = this._dirFileID;
            FileItem fItem = new FileItem(imgFullName);
            req.Img = fItem;
            //req.Title = imgName;
            req.ImageInputTitle = imgName;
            PictureUploadResponse response = _client.Execute(req, _sessionKey);
            if (response.IsError)  //请求没有成功。
            {
                return "";
            }
            else
            {
                return response.Picture.PicturePath;
            }
        }

























        //public string GetSessionKey()
        //{
        //    WebBrowser browser = new System.Windows.Forms.WebBrowser();
        //    browser.Url = new Uri(_procUrl);
        //    browser.Navigate(_procUrl);
        //    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetHtmlDocument);
        //    return "";
        //}
        //private void GetHtmlDocument(object sender,WebBrowserDocumentCompletedEventArgs e)
        //{
        //    WebBrowser browser=sender as WebBrowser;
        //    string str = browser.DocumentText;
        //}
    }
}
